<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/17 0017
 * Time: 下午 3:57
 */
namespace app\auth\model;
use think\console\command\make\Model;
use think\Db;

class Role extends Model{
    /**
     * 更改角色信息
     * @param $data
     * @return int|string
     */
    public function role_update($data){
        Db::startTrans();
        $update = Db::name('role')->where(['role_id'=>$data['role_id'],'del'=>1])->update($data);

        #查权限路径
        $auth_info = Db::name('auth')
            ->field('auth_c,auth_a')
            ->select($data['role_auth_ids']);

        #拼装控制器和方法
        $s='';
        foreach ($auth_info as $v){
            if(!empty($v['auth_c']))
                $s.= $v['auth_c']."-".$v['auth_a'].",";

        }

        #除去右边的逗号
        $s = rtrim($s,',');

        #更新角色分配权限
        $update1 = Db::name('role')
            ->where(['role_id'=>$data['role_id']])
            ->update(['role_auth_ac'=>$s]);

        #返回修改信息
        if($update>0 || $update1>0){
            Db::commit();
            return ['code'=>200,'msg'=>config('msg.200')];
        }
        else{
            Db::rollback();
            return ['code'=>10025,'msg'=>config('msg.10025')];
        }
    }

    /**
     * 添加角色
     * @param $data
     * @return int|string
     */
    public function role_add($data){
        $add = Db::name('role')->insertGetId($data);
        return $add;
    }

    /**
     * 删除角色
     * @param $data
     * @return int|string
     */
    public function role_del($data){
        $del = Db::name('role')->where(['role_id'=>['in',$data['role_id']],'del'=>1])->update(['del'=>0]);
        return $del;
    }

    /**
     * 更改用户角色
     * @param $data
     * @return int|string
     */
    public function user_role_update($data){
        $up = Db::name('member')
            ->where(['id'=>$data['id']])->update(['identity'=>$data['identity']]);
        return $up;
    }

    /**
     * 角色列表
     * @param $data
     * @return array
     */
    public function role_list($data){
        #条件
        $where = 'del=1';
        $where.= isset($data['keyword'])? ' and CONCAT(role_name) like "%'.$data['keyword'].'%"':'';
        #总条数
        $total = Db::name('role')->where($where)->count();
        #总页数
        $page = ceil($total/$data['limits']);
        if ($data['curr']>$page){
            $data['curr']=$page;
        }
        #列表
        $list = Db::name('role')
            ->where($where)->page($data['curr'],$data['limits'])->select();
        foreach ($list as $k =>$v){
            $name =Db::name('auth')->where(['auth_id'=>['in',$v['role_auth_ids']]])->field('auth_name')->column('auth_name');
            $list[$k]['name']=implode(',',$name);
        }

        #返回值
        $result = [
            'list'  =>$list,
            'page'  =>$page,
            'limits'=>$data['limits'],
            'total' =>$total,
            'curr'  =>$data['curr']
        ];
        return $result;
    }
}